package project.vip.action;

import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.PageActionSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import project.vip.po.Ad;
import project.vip.services.AdminAdService;

public class AdminAdAction extends PageActionSupport {
 
    /**	
     * Member Description
     */
    
    

    private static final Log logger = LogFactory.getLog(AdminAdAction.class);

    /**	
     * Member Description
     */

    private AdminAdService adminAdService;
    

    
    private String id;

    private String name;
    
    private String img;
    
    private int order;
    
    private String url;
    
    // false 下架
    private boolean shelves = true;

    public String view() {
        this.pageSize = 20;
        page = adminAdService.pagedQuery(pageNo, pageSize,"");
        super.page = page;
        this.tabs = this.bulidTabs();
        return "view";

    }
    
    public String toAdd() {
        return "add";
    }

    public String add() {
        if (StringUtils.isNullOrEmpty(name)) {
            view();
            return "view";
        }
        Ad item = new Ad();
        item.setName(name);
        item.setShelves(shelves);
        item.setImg(img);
        item.setOrder(order);
        item.setUrl(url);
        try {
            adminAdService.save(item);
            view();
            this.message = "添加成功。";
        } catch (BusinessException e) {
            this.error = e.getMessage();
            return "add";
        } catch (Throwable t) {
            logger.error("adminAdService.save(item) fail ", t);
            this.message = "系统繁忙，请稍后重试";
            return "add";
        }
        return "view";
    }

    public String toUp() {
        Ad item = adminAdService.getById(id);
        name = item.getName();
        shelves = item.getShelves();
        img = item.getImg();
        order = item.getOrder();
        url = item.getUrl();
        return "update";
    }

    public String update() {
        try {
            Ad item =  adminAdService.getById(id);
            item.setName(name);
            item.setShelves(shelves);
            item.setImg(img);
            item.setOrder(order);
            item.setUrl(url);
            adminAdService.update(item);
            view();
            this.message = "修改成功。";
        } catch (BusinessException e) {
            this.error = e.getMessage();
        } catch (Throwable t) {
            logger.error("adminAdService.update(item) fail ", t);
            this.message = "系统繁忙，请稍后重试";
            return "update";
        }
        return "view";
    }
    
    public String del() {
        try {
            adminAdService.delete(id);
            this.message = "删除成功。";
        } catch (BusinessException e) {
            this.error = e.getMessage();
        } catch (Throwable t) {
            logger.error("adminAdService.update(item) fail ", t);
            this.message = "系统繁忙，请稍后重试";
        }
        view();
        return "view";
    }
    
    

    public void setAdminAdService(AdminAdService adminAdService) {
        this.adminAdService = adminAdService;
    }

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the img
	 */
	public String getImg() {
		return img;
	}

	/**
	 * @param img the img to set
	 */
	public void setImg(String img) {
		this.img = img;
	}

	/**
	 * @return the order
	 */
	public int getOrder() {
		return order;
	}

	/**
	 * @param order the order to set
	 */
	public void setOrder(int order) {
		this.order = order;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the shelves
	 */
	public boolean isShelves() {
		return shelves;
	}

	/**
	 * @param shelves the shelves to set
	 */
	public void setShelves(boolean shelves) {
		this.shelves = shelves;
	}

	/**
	 * @return the adminAdService
	 */
	public AdminAdService getAdminAdService() {
		return adminAdService;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

   
    

}
